package fr.uvsq.isty.bdh.hchess.gui.game;

import fr.uvsq.isty.bdh.hchess.engine.Engines;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import javax.swing.*;

/**
 * 
 * @author Florent Hemmi <florent.hemmi@gmail.com>
 */
public class RemoveEngineDialog extends JDialog {
    
    private JPanel container;
    private JPanel cNoEngine;
    private JLabel label;
    private JLabel noEngine;
    private JComboBox combo;
    private JButton validate;
    String lastItemSelected;
    
    
    public RemoveEngineDialog(JFrame parent, String title, boolean modal) {
        super(parent, title, modal);
        try {
            if (Engines.getInstance().getNbEngineInstalled() == 0) {
                this.setSize(160, 60);
            } else {
                this.setSize(160, 100);
            }
        } catch (IOException ex) {}
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        
        label = new JLabel("Engine :");
        noEngine = new JLabel("No engine installed!");
        combo = new JComboBox();
        validate = new JButton("Remove");
        
        container = new JPanel();
        container.setLayout(new FlowLayout());
        container.add(label);
        container.add(combo);
        container.add(validate);
        
        
        cNoEngine = new JPanel();
        cNoEngine.setLayout(new FlowLayout());
        Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
        noEngine.setFont(font);
        noEngine.setForeground(Color.RED);
        cNoEngine.add(noEngine);
        
        combo.addItemListener(new ItemState());
        
        validate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Engines.getInstance().deleteEngine(lastItemSelected);
                    updateComboBox();
                    if (Engines.getInstance().getNbEngineInstalled() == 0) {
                        RemoveEngineDialog.this.setVisible(false);
                    }
                } catch (IOException ex) {
                    System.out.println("test");
                    JOptionPane.showMessageDialog(null, "Cannot remove the engine.\n"+
                            "The file is propably in read-only.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        
        try {
            if (Engines.getInstance().getNbEngineInstalled() == 0) {
               this.setContentPane(cNoEngine);
            }
            else {
                updateComboBox();
                this.setContentPane(container);
            }
        } catch (IOException ex) {}
        
        this.setVisible(true);
    }
    
    private void updateComboBox() {
        try {
            combo.removeAllItems();
            for (String s : Engines.getInstance().list()) {
                if (lastItemSelected == null) {
                    lastItemSelected = s;
                }
                combo.addItem(s);
            }
        } catch (IOException ex) {}
    }
    
    class ItemState implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            lastItemSelected = e.getItem().toString();
        }
    }
}